<?php

declare(strict_types=1);
namespace App\Controller\MchApi;
use App\Exception\XZXValidationException;
use App\Middleware\MchApi\MchJWTAuthMiddleware;
use App\Service\SysAnnouncementService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Hyperf\Contract\TranslatorInterface;

/**
 * Class SysAnnouncementController
 * @package App\Controller
 * @Controller(prefix="/mch/api/sysAnnouncement")
 */
class SysAnnouncementController extends MchBaseController
{

    /**
     * @Inject
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    /**
     * @Inject()
     * @var SysAnnouncementService
     */
    protected $SysAnnouncementService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct(LoggerFactory $loggerFactory)
    {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    private function setLocale () {
        if ($this->request->header('locale') == 'en') {
            $this->translator->setLocale('en');
        }
    } 

    /**
     * 查看
     * @PostMapping(path = "get")
     * @Middleware(MchJWTAuthMiddleware::class)
     */
    public function get(RequestInterface $request)
    {
        $this->setlocale();
        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'id' => 'required|uuid',
            ]
        );

        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();
        $ext = $this->request->getAttribute("ext");
        $param['uid'] =  $ext['uid'];
        return $this->SysAnnouncementService->getForMch($param);
    }

    /**
     * 查询
     * @PostMapping(path = "list")
     * @Middleware(MchJWTAuthMiddleware::class)
     */
    public function list(RequestInterface $request)
    {
        $this->setlocale();
        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'page'          => 'required|integer|min:1',
                'limit'         => 'required|integer|min:1',
                'search.title'  => 'sometimes|required|between:2,20|alpha_num',
            ]
        );

        if ($validator->fails()){
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();
        $ext = $this->request->getAttribute("ext");
        $param['uid']       =  $ext['uid'];
        $param['type'] = 2;
        return $this->SysAnnouncementService->listForIndex($param);
    }

    /**
     * 已读
     * @PostMapping(path = "read")
     * @Middleware(MchJWTAuthMiddleware::class)
     */
    public function read(RequestInterface $request)
    {
        $this->setlocale();
        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'sysAnnouncementId' => 'required|uuid',
            ]
        );

        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();
        $ext = $this->request->getAttribute("ext");
        $param['uid']       =  $ext['uid'];
        $param['source']    =  1;
        return $this->SysAnnouncementService->read($param);
    }


    /**
     * 查看
     * @PostMapping(path = "getBankAccount")
     * @Middleware(MchJWTAuthMiddleware::class)
     */
    public function getBankAccount(RequestInterface $request)
    {
        $this->setlocale();
        $validator = $this->validationFactory->make(
            $request->all(),
            [
                // 'id' => 'required|uuid',
            ]
        );

        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();
        $ext = $this->request->getAttribute("ext");
        $param['uid'] =  $ext['uid'];
        $param['type'] = 2;
        return $this->SysAnnouncementService->getBankAccount($param);
    }

}